package com.equipmentmanage.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.equipmentmanage.model.PageBean;
import com.equipmentmanage.model.User;
import com.equipmentmanage.service.DepartmentService;
import com.equipmentmanage.service.UserService;
import com.equipmentmanage.util.PageUtil;
import com.equipmentmanage.util.ResponseUtil;
import com.equipmentmanage.util.StringUtil;
/*     */ @Controller
/*     */ @RequestMapping({"/user"})
/*     */ public class UserController
/*     */ {
/*     */ 
/*     */   @Autowired
/*     */   private UserService userService;
/*     */ 
/*     */   @Autowired
/*     */   private DepartmentService departmentService;
/*     */ 
/*     */   @RequestMapping({"/login"})
/*     */   public String login(User user, HttpServletRequest request)
/*     */   {
/*  43 */     User resultUser = this.userService.login(user);
/*  44 */     if (resultUser.getId() == null) {
/*  45 */       request.setAttribute("user", user);
/*  46 */       request.setAttribute("errorMsg", "用户名或密码错误！");
/*  47 */       return "login";
/*     */     }
/*  49 */     HttpSession session = request.getSession();
/*  50 */     session.setAttribute("currentUser", resultUser);
/*  51 */     return "redirect:/main.jsp";
/*     */   }
/*     */ 
/*     */   @RequestMapping({"/logout"})
/*     */   public String logout(HttpSession session)
/*     */   {
/*  57 */     session.invalidate();
/*  58 */     return "redirect:/login.jsp";
/*     */   }
/*     */ 
/*     */   @RequestMapping({"/list"})
/*     */   public ModelAndView list(@RequestParam(value="page", required=false) String page, User s_user, HttpServletRequest request)
/*     */   {
/*  64 */     ModelAndView mav = new ModelAndView();
/*  65 */     HttpSession session = request.getSession();
/*  66 */     if (StringUtil.isEmpty(page)) {
/*  67 */       page = "1";
/*  68 */       session.setAttribute("s_user", s_user);
/*     */     } else {
/*  70 */       s_user = (User)session.getAttribute("s_user");
/*     */     }
/*  72 */     PageBean pageBean = new PageBean(Integer.parseInt(page), 3);
/*  73 */     List userList = this.userService.find(pageBean, s_user);
/*  74 */     int total = this.userService.count(s_user);
/*  75 */     String pageCode = PageUtil.getPagation(request.getContextPath() + "/user/list.do", total, Integer.parseInt(page), 3);
/*  76 */     mav.addObject("pageCode", pageCode);
/*  77 */     mav.addObject("modeName", "用户管理");
/*  78 */     mav.addObject("userList", userList);
/*  79 */     mav.addObject("mainPage", "jsp/user/list.jsp");
/*  80 */     mav.setViewName("main");
/*  81 */     return mav;
/*     */   }
/*     */ 
/*     */   @RequestMapping({"/preSave"})
/*     */   public ModelAndView preSave(@RequestParam(value="id", required=false) String id) {
/*  86 */     ModelAndView mav = new ModelAndView();
/*  87 */     mav.addObject("mainPage", "jsp/user/save.jsp");
/*  88 */     mav.addObject("modeName", "用户管理");
/*  89 */     mav.setViewName("main");
/*  90 */     if (StringUtil.isNotEmpty(id)) {
/*  91 */       mav.addObject("actionName", "用户修改");
/*  92 */       User user = this.userService.loadById(Integer.parseInt(id));
/*  93 */       mav.addObject("user", user);
/*     */     } else {
/*  95 */       mav.addObject("actionName", "用户添加");
/*     */     }
/*  97 */     List departmentList = this.departmentService.find(null, null);
/*  98 */     mav.addObject("departmentList", departmentList);
/*  99 */     return mav;
/*     */   }
/*     */ 
/*     */   @RequestMapping({"/save"})
/*     */   public String save(User user) {
/* 104 */     if (user.getId() == null)
/* 105 */       this.userService.add(user);
/*     */     else {
/* 107 */       this.userService.update(user);
/*     */     }
/* 109 */     return "redirect:/user/list.do";
/*     */   }
/*     */ 
/*     */   @RequestMapping({"/delete"})
/*     */   public void delete(@RequestParam("id") String id, HttpServletResponse response) throws Exception {
/* 114 */     this.userService.delete(Integer.valueOf(id).intValue());
/* 115 */     JSONObject result = new JSONObject();
/* 116 */     result.put("success", Boolean.valueOf(true));
/* 117 */     ResponseUtil.write(result, response);
/*     */   }
/*     */ }

/* Location:           C:\Users\arcgis\Desktop\新建文件夹\a0\[www.java1234.com]《SpringMvc设备管理系统》第零讲源码及文档\EquipmentSys\WEB-INF\classes\
 * Qualified Name:     com.java1234.controller.UserController
 * JD-Core Version:    0.6.1
 */